package com.billionsfinance.hessian.common.zookeeper.client;

import com.billionsfinance.hessian.common.machine.MachineResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ FileName: ClientSideZkEventConsumer.java
 * @ Author: WeiHui-Z
 * @ Date: 2016-11-21  14:30
 * @ Version: v1.0.0
 */
public class ClientSideZkEventConsumer implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientSideZkEventConsumer.class);

    @Override
    public void run() {
        //循环消费
        while (true) {
            ClientSideZkEvent event = ClientSideZkEventQueue.take();

            // 拿到了event，有效，怎么处理?
            ClientSideZkEventType type = event.getType();
            String serviceWithEdition = event.getService();
            String data = event.getData();
            LOGGER.info("------------------------------------------");
            LOGGER.info("type: " + type);
            LOGGER.info("service:" + serviceWithEdition);
            LOGGER.info("data:" + data);
            LOGGER.info("------------------------------------------");

            if (type == ClientSideZkEventType.ADD) {
                MachineResource.getInstance().addMachineResource(serviceWithEdition, data);
            }
            if (type == ClientSideZkEventType.REMOVE) {
                MachineResource.getInstance().removeMachineResource(serviceWithEdition, data);
            }
        }
    }
}
